import 'dart:math';

import 'package:flutter/material.dart';
import 'package:totaldemo/pages/bottom_sheet.dart';
import 'package:totaldemo/practice/textField.dart';
import 'package:get/get.dart';

class ClickSubIiemPage extends StatefulWidget {
  const ClickSubIiemPage({Key key}) : super(key: key);

  @override
  _ClickSubIiemPageState createState() => _ClickSubIiemPageState();
}

class _ClickSubIiemPageState extends State<ClickSubIiemPage> {
  int _counter = 0;
  List<dynamic> pracItem = [];
//文本输入框
  TextEditingController inputController = TextEditingController();

//
  List gridTitleList = [];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('点击增加一个item'),
      ),
      body: Stack(
        children: [
          Column(
            children: [
              Container(
                height: 500,
                child: Expanded(
                  child: GridView.builder(
                      gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                          crossAxisCount: 3),
                      itemCount: pracItem.length,
                      itemBuilder: (BuildContext context, int index) {
                        return Stack(
                          children: [
                            Container(
                              color: Color.fromARGB(
                                255,
                                Random.secure().nextInt(200),
                                Random.secure().nextInt(200),
                                Random.secure().nextInt(200),
                              ),
                              child: Center(
                                  child:
                                      Text('${pracItem[index]['gridTitle']}')),
                            ),
                            Positioned(
                              right: 0,
                              top: 10,
                              bottom: 0,
                              child: GestureDetector(
                                onTap: () {
                                  print('可以删除');
                                  print(pracItem);
                                  setState(() {
                                    pracItem.removeAt(index);
                                  });
                                },
                                child: Icon(
                                  Icons.delete,
                                  color: Colors.red,
                                ),
                              ),
                            ),
                          ],
                        );
                      }),
                ),
              ),
              Expanded(child: Container()),
              Positioned(
                left: 0,
                bottom: 0,
                child: Container(
                  width: MediaQuery.of(context).size.width,
                  height: 50,
                  color: Colors.red,
                  // onTap: _addItem,
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: [
                      Expanded(
                        child: TextField(
                          controller: inputController,
                          decoration: InputDecoration(
                            hintText: '请输入标题',
                            border: InputBorder.none,
                            // hintText: _currentTipsText,
                            contentPadding:
                                EdgeInsets.only(left: 10, top: 17, bottom: 17),
                          ),
                        ),
                      ),
                      InkWell(
                        onTap: _addItem,
                        child: Container(
                            height: 50,
                            width: 50,
                            alignment: Alignment.center,
                            child: Icon(Icons.send)),
                      )
                    ],
                  ),
                ),
              ),
            ],
          ),
        ],
      ),
    );
  }

  _addItem() {
    print('输入框的内容是' + inputController.text);
    setState(() {
      // gridTitle = inputController.text;

      if (inputController.text != null) {
        pracItem.add({'gridTitle': inputController.text});
      } else {}
      inputController.text = '';
    });
  }
}
